package entry;

import frame.laxcen.BaseFrame;
import frame.laxcen.exception.IllegalFrameFormatException;
import frame.laxcen.request.Request_0001_00;
import frame.laxcen.request.Request_0010_10;
import frame.laxcen.request.Request_0010_FF;
import frame.laxcen.response.*;
import frame.laxcen.util.FrameUtil;
import tcpclient.v1.RFIDClient;
import util.Ref;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

public class EntryV1 {

    public static void test1() throws IOException, IllegalFrameFormatException {

        RFIDClient client = new RFIDClient();

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("连接成功！");
        }

        if(client.trySend(new Request_0001_00().FrameBytes, 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("发送成功！");
        }

        Ref<BaseFrame> ref = Ref.empty();

        if(client.waitForArbitraryResponse(1024, 8000, ref) == RFIDClient.AsyncResult.Success) {

            FrameUtil.printFrame(ref.get());
        }
    }

    public static void test2() throws IOException, IllegalFrameFormatException {

        RFIDClient client = new RFIDClient();

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("连接成功！");
        }

        if(client.trySend(new Request_0001_00().FrameBytes, 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("发送成功！");
        }

        Ref<BaseFrame> ref = Ref.empty();

        if(client.waitForSpecialResponse(1024, 1500, ref, Response_0001_00.class) == RFIDClient.AsyncResult.Success) {

            new Response_0001_00(ref.get()).explainToMap().forEach((k, v) -> System.out.println(k + ": " + v));
        }
    }

    public static void test3() throws IOException, IllegalFrameFormatException, InterruptedException {

        RFIDClient client = new RFIDClient();

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("连接成功！");
        }

        for(int i = 0; i < 100; ++i) {

            if(client.trySend(new Request_0001_00().FrameBytes, 3000) == RFIDClient.AsyncResult.Success) {

                System.out.println("发送成功！");
            }

            Ref<BaseFrame> ref = Ref.empty();

            if(client.waitForSpecialResponse(1024, 1500, ref, Response_0001_00.class) == RFIDClient.AsyncResult.Success) {

                new Response_0001_00(ref.get()).explainToMap().forEach((k, v) -> System.out.println(k + ": " + v));
            }
        }
    }

    public static void test4() throws IOException, IllegalFrameFormatException, InterruptedException {

        RFIDClient client = new RFIDClient();

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("连接成功！");
        }

        if(client.trySend(new Request_0010_10().FrameBytes, 3000) == RFIDClient.AsyncResult.Success) {

            System.out.println("发送成功！");
        }

        List<BaseFrame> a = new LinkedList<>(), b = new LinkedList<>();

        RFIDClient.AsyncResult result = client.waitForResponseListSplitByUploadFlag(1024, 3000, a, b);

        System.out.println(result);

        Function<BaseFrame, Map<String, String>> explainFrame = (frame) -> {

            try {

                return new Response_0010_10(frame).explainToMap();

            } catch (IllegalFrameFormatException e) {

                e.printStackTrace();
            }

            return Collections.emptyMap();
        };

        Consumer<Map<String, String>> printExplain = (map) -> map.forEach((k, v) -> System.out.println(k + ": " + v));

        System.out.println("> Not Upload");

        if(!a.isEmpty()) a.stream().map(explainFrame).forEach(printExplain);

        System.out.println("> Is Upload");

        if(!b.isEmpty()) b.stream().map(explainFrame).forEach(printExplain);
    }

    public static void main(String[] args) throws Exception {

    }
}

